package com.huangcy.gulimall.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.huangcy.gulimall.order.entity.OrderEntity;
import com.huangcy.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huangcy.common.utils.PageUtils;
import com.huangcy.common.utils.Query;

import com.huangcy.gulimall.order.dao.OrderItemDao;
import com.huangcy.gulimall.order.entity.OrderItemEntity;
import com.huangcy.gulimall.order.service.OrderItemService;


@Service("orderItemService")
@RabbitListener(queues = {"hello-java-queue"})
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queues:声明需要监听的队列
     * org.springframework.amqp.core.Message
     * <p>
     * 参数可以写以下类型
     * 1、Message message,原生消息详细信息。头+体
     * 2、T<发送的消息的类型>OrderReturnReasonEntity content
     * 3、Channel channel:当前传输数据的通道
     * 场景
     * 1)、订单服务启动多个；同一个消息不能被重复提交。
     * 2)、只有一个消息完全处理完，方法运行结束，我们就可以接受到下一条消息
     * Queue：可以有很多人监听。只要收到消息，队列就会删除消息，而且只能有一个人收到这条消息
     *
     * @param message
     */
//    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnReasonEntity content, Channel channel) throws InterruptedException {
        System.out.println("接收消息..内容" + content);
        // channel内按顺序自增的
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println(deliveryTag);
        // 签收消息 ,multiple 批量模式
        // basicAck(long deliveryTag, boolean multiple)
        try {
            if (deliveryTag % 2 == 0) {
                // 收货
                channel.basicAck(deliveryTag, false);
                System.out.println("签收了货物..." + deliveryTag);
            } else {
                // 退货  requeue=false 丢弃货物 requeue=true 消息重新入队
                //basicNack(long deliveryTag, boolean multiple, boolean requeue)
                channel.basicNack(deliveryTag, false, false);
                //basicReject(long deliveryTag, boolean requeue)
//                channel.basicReject(deliveryTag, false);
                System.out.println("没有签收的货物..." + deliveryTag);
            }

        } catch (IOException e) {
            // 网络中断了
        }
//        Thread.sleep(3000);
        System.out.println("消息处理完成=》" + content.getName());

    }

    @RabbitHandler
    public void recieveMessage2(OrderEntity content) throws InterruptedException {
        System.out.println("接收消息..内容" + content);


    }

}